C++- CPP教程

C++ 异常处理库 <stdexcept>

<stdexcept> 是 C++ 标准库中的一个头文件,它定义了一组标准异常类,用于处理程序运行时的错误情况。

异常是程序运行时发生的错误,它们可以被捕获并处理,以避免程序的非正常终止。 <stdexcept> 头文件定义了一组从 std::exception 派生的异常类,这些类提供了一种标准的方式来报告和处理错误。

语法

要使用 <stdexcept> 中的异常类,首先需要包含这个头文件:

#include <stdexcept>

然后,你可以使用这些异常类来抛出和捕获异常。例如:

throw std::runtime_error("An error occurred");

异常类

<stdexcept> 头文件定义了以下异常类:

  • std::exception :所有标准异常类的基类。
  • std::bad_exception :当异常处理过程中发生错误时抛出。
  • std::bad_alloc :当内存分配失败时抛出。
  • std::bad_cast :当类型转换失败时抛出。
  • std::bad_typeid :当 typeid 操作失败时抛出。
  • std::logic_error :当逻辑错误发生时抛出,例如无效的输入参数。
  • std::domain_error :当函数调用的参数不在有效范围内时抛出。
  • std::invalid_argument :当函数调用的参数无效时抛出。
  • std::length_error :当容器操作因为长度限制而失败时抛出。
  • std::out_of_range :当访问容器的非法索引时抛出。
  • std::overflow_error :当算术运算导致溢出时抛出。
  • std::range_error :当函数返回值不在期望的范围内时抛出。
  • std::underflow_error :当算术运算导致下溢时抛出。

实例

下面是一个使用 <stdexcept> 的简单示例,演示了如何抛出和捕获异常:

示例代码
#include <iostream>#include <stdexcept>voidriskyFunction(intx){if(x<0){throwstd::invalid_argument("Negative value not allowed");}std::cout<<"Processing "<<x<<std::endl;}intmain(){try{riskyFunction(-1);}catch(conststd::invalid_argument&e){std::cerr<<"Caught an exception: "<<e.what()<<std::endl;}return0;}

输出结果:


Caught an exception: Negative value not allowed

在这个示例中, riskyFunction 函数检查传入的参数是否为负数,如果是,则抛出 std::invalid_argument 异常。在 main 函数中,我们使用 try-catch 块来捕获并处理这个异常。


标准异常类

std::exception

std::exception 是所有标准异常类的基类。它提供了一个虚函数 what(),用于返回描述异常的字符串。

示例代码
#include <iostream>#include <exception>intmain(){try{throwstd::exception();}catch(conststd::exception&e){std::cout<<"Caught exception: "<<e.what()<<std::endl;}return0;}

std::logic_error

std::logic_error 派生自 std::exception,表示程序逻辑错误。它是一个抽象基类,通常由派生类来具体化。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::logic_error("Logic error occurred");}catch(conststd::logic_error&e){std::cout<<"Caught logic error: "<<e.what()<<std::endl;}return0;}

std::invalid_argument

std::invalid_argument 派生自 std::logic_error,表示传递了无效的参数。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::invalid_argument("Invalid argument provided");}catch(conststd::invalid_argument&e){std::cout<<"Caught invalid argument: "<<e.what()<<std::endl;}return0;}

std::domain_error

std::domain_error 派生自 std::logic_error,表示参数超出了有效范围。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::domain_error("Domain error occurred");}catch(conststd::domain_error&e){std::cout<<"Caught domain error: "<<e.what()<<std::endl;}return0;}

std::length_error

std::length_error 派生自 std::logic_error,表示长度错误,如在容器操作中超出了最大长度限制。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::length_error("Length error occurred");}catch(conststd::length_error&e){std::cout<<"Caught length error: "<<e.what()<<std::endl;}return0;}

std::out_of_range

std::out_of_range 派生自 std::logic_error,表示访问超出了有效范围。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::out_of_range("Out of range error occurred");}catch(conststd::out_of_range&e){std::cout<<"Caught out of range error: "<<e.what()<<std::endl;}return0;}

std::runtime_error

std::runtime_error 派生自 std::exception,表示程序运行时错误。它是一个抽象基类,通常由派生类来具体化。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::runtime_error("Runtime error occurred");}catch(conststd::runtime_error&e){std::cout<<"Caught runtime error: "<<e.what()<<std::endl;}return0;}

std::range_error

std::range_error 派生自 std::runtime_error,表示计算结果超出了表示范围。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::range_error("Range error occurred");}catch(conststd::range_error&e){std::cout<<"Caught range error: "<<e.what()<<std::endl;}return0;}

std::overflow_error

std::overflow_error 派生自 std::runtime_error,表示算术溢出错误。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::overflow_error("Overflow error occurred");}catch(conststd::overflow_error&e){std::cout<<"Caught overflow error: "<<e.what()<<std::endl;}return0;}

std::underflow_error

std::underflow_error 派生自 std::runtime_error,表示算术下溢错误。

示例代码
#include <iostream>#include <stdexcept>intmain(){try{throwstd::underflow_error("Underflow error occurred");}catch(conststd::underflow_error&e){std::cout<<"Caught underflow error: "<<e.what()<<std::endl;}return0;}

使用自定义异常类

除了标准异常类,我们还可以定义自己的异常类,继承自标准异常类。

示例代码
#include <iostream>#include <stdexcept>classMyException:publicstd::runtime_error{public:MyException(conststd::string&message):std::runtime_error(message){}};intmain(){try{throwMyException("My custom exception occurred");}catch(constMyException&e){std::cout<<"Caught custom exception: "<<e.what()<<std::endl;}return0;}

<stdexcept> 是 C++ 标准库中一个重要的组成部分,它提供了一组标准异常类,使得错误处理更加一致和可预测。通过使用这些异常类,你可以编写更加健壮和易于维护的代码。对于初学者来说,理解异常的基本概念和如何使用 <stdexcept> 中的异常类是非常重要的。